#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   dns_rndc_remote.sh
# Version:    1.0
# Date:       2013/10/29
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-06DNS-01字符模式-05使用rndc控制DNS服务-02使用rndc控制远程DNS服务器
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/10/29
#             - Start named service, rndc control remote dns service
# ----------------------------------------------------------------------

##! @TODO: local-dns service, remote-rndc
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_setup()
{
    trap do_cleanup INT EXIT
    serv=named
    CONF="/etc/named.conf"
    RNDCKEY="/etc/rndc.key"

    # backup /etc/named.conf,rndc.key
    cp ${CONF} ${CONF}.bak
    EchoResult "backup ${CONF}"
 
    cp ${RNDCKEY} ${RNDCKEY}.bak
    EchoResult "backup ${RNDCKEY}"

    # check owner of /etc/named.conf
    OWNER=`ls -l ${CONF} | awk '{print $3,$4}'`
    if [ "${OWNER}" != "named named" ]; then
        chown named:named ${CONF}
        EchoResult "check owner of ${CONF}"
    fi

    OWNER=`ls -l ${RNDCKEY} | awk '{print $3,$4}'`
    if [ "${OWNER}" != "named named" ]; then
        chown named:named ${RNDCKEY}
        EchoResult "check owner of ${RNDCKEY}"
    fi

}


##! @TODO:  test rndc on remote
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function do_test()
{
    DNS_SERVER=`ifconfig | grep 'inet ' | grep -m 1 -v '127.0.0.1'  | awk '{print $2}' | tr -d "addr:"`

    #rndc-confgen on local
    RNDCCONF="/etc/rndc.conf"
    [ -f ${RNDCCONF} ] && rm -f ${RNDCCONF}

    # generate rndc.key on local
    rndc-confgen  > ${RNDCCONF}

    # rcp rndc.conf to remote
    ${RCP} ${RNDCCONF} ${RUSERNAME}@${RHOST}:${RNDCCONF}
    ##
    
    # update rndc.key on local
    TMP_KEY="/tmp/key$$"
    grep -E "^# key" -A 8 ${RNDCCONF} > ${TMP_KEY}
    
    sed -i  -e 's/^# //g' -e 's/rndc-key/rndc-key-remote/g' -e "s/inet 127.0.0.1/inet ${DNS_SERVER}/g" -e "s/127.0.0.1/${RHOST}/g" ${TMP_KEY}

    # echo tmp_key to rndc.key
    cat ${TMP_KEY} >> ${RNDCKEY}


    # add include /etc/rndc.key in named.conf
    echo "include \"${RNDCKEY}\";" >> ${CONF}

    sleep 3

    # restart dns service
    restart_daemon ${serv}

    PID_NAMED=`pidof ${serv}`

    sleep 3
    # check httpd start
    status_daemon ${serv}  && netstat -nlutp | grep ${serv}
    EchoResult "service ${serv} start"


    ########################
    # setup rndc.conf on remote
    ${RSH} -l root -n ${RHOST} "sed -i -e '/^# /d' -e 's/rndc-key/rndc-key-remote/g' -e \"s/127.0.0.1/${DNS_SERVER}/g\" ${RNDCCONF}"


    # rndc
    ${RSH} -l root -n ${RHOST} "/usr/sbin/rndc status; /usr/sbin/rndc stop"

    sleep 3

    # check log messages
    grep "named\[${PID_NAMED}\]: exiting" /var/log/messages && grep "named\[${PID_NAMED}\]: received control channel command 'stop'" /var/log/messages
    EchoResult "remote control rndc"

}

##! @TODO:  cleanup tmp files, back up named.conf and stop named service.
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure
function do_cleanup()
{
    stop_daemon ${serv}
    [ -f ${CONF}.bak ] && mv -f ${CONF}.bak ${CONF}
    rm -f ${RNDCCONF} ${TMP_KEY}
    [ -f ${RNDCKEY}.bak ] && mv -f ${RNDCKEY}.bak ${RNDCKEY}

    ${RSH} -l root -n ${RHOST} "rm -f ${RNDCCONF}"

}
# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh
. ${SFROOT}/lib/XmlParse.sh

. ${CMDLIB}/setup.sh

# get RHOST, RUSERNAME, PASSWORD from config.xml
DATA_FILE="${SFROOT}/testcases/Network_Service/config.xml"
[ -e $DATA_FILE ] || { echo "$DATA_FILE not find, test end."; exit 1; }

XmlParse ${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

export RHOST=${REHOST}
export RUSERNAME=${RUSERNAME}
export PASSWORD=${PASSWORD}

setup_env || { echo "setup env failed, test end."; exit 1; }

### main program
do_setup

do_test
if [ $? -ne 0 ];then
    echo "failed: rndc remote dns service, pls check datetime"
    exit 1
else
    echo "pass: rndc remote dns service"
fi
